package com.example.activitytest

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_second.*

class SecondActivity : AppCompatActivity() {
    val TAG : String = "SecondActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        var extradata = intent.getStringExtra("extra_data")
        Log.d(TAG, "onCreate: extra data is $extradata")
        textview1.text = extradata
        button2.setOnClickListener {
            Toast.makeText(this,"点击button2 ",Toast.LENGTH_SHORT).show()
            val intent2 = Intent()
            intent2.putExtra("data_return","Hello mainActivity")
            setResult(RESULT_OK,intent2)
            finish()
        }

    }

    //当用户点击返回时的调用函数，假设不通过点击按钮返回，也可以在这里返回
    override fun onBackPressed() {
        val intent2 = Intent()
        intent2.putExtra("data_return","Hello mainActivity")
        setResult(RESULT_OK,intent2)
        finish()
    }
}